using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class ComponentSubMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.cpnts_Menu, new Point(0, -107));

    private static List<dynamic> drawableList = new List<object>(new string[1] { string.Empty });
    private static List<dynamic> textureList = new List<object>(new string[1] { string.Empty });
    private static List<dynamic> paletteList = new List<object>(new string[1] { string.Empty });
    private static UIMenuListItem drawableListItem;
    private static UIMenuListItem textureListItem;
    private static UIMenuListItem paletteListItem;
    private static int cid;

    public ComponentSubMenu()
    {
        Main.pool.Add(menu);
        menu.OnListChange += OnListChange;
        menu.OnItemSelect += OnItemSelect;
        menu.OnMenuClose += OnMenuClose;
    }

    public static void InitComponentSubMenu(int _cid)
    {
        cid = _cid;
        UpdateComponentsValue();
        menu.Clear();
        drawableList = new List<object>();
        textureList = new List<object>();
        paletteList = new List<object>();
        Skin.Component component;
        int[] textureData;
        if (DressingScript.Dressing == Main.Member.P2)
        {
            component = Main.P2.skin.Components[cid];
            textureData = Main.P2.skin.ComponentsTextureData[cid];
        }
        else
        {
            component = Main.P1.skin.Components[cid];
            textureData = Main.P1.skin.ComponentsTextureData[cid];
        }
        for (int i = 0; i < component.drawableNumber; i++) drawableList.Add(i);
        for (int j = 0; j < textureData[component.drawableId]; j++) textureList.Add(j);
        for (int k = 0; k <= 3; k++) paletteList.Add(k);
        drawableListItem = new UIMenuListItem(TextSetting.cpnts_ListItem_Drawable, drawableList, component.drawableId);
        textureListItem = new UIMenuListItem(TextSetting.cpnts_ListItem_Texture, textureList, component.textureId);
        paletteListItem = new UIMenuListItem(TextSetting.cpnts_ListItem_Palette, paletteList, component.paletteId);
        menu.SetBannerType(new UIResRectangle());
        menu.AddItem(drawableListItem);
        menu.AddItem(textureListItem);
        menu.AddItem(paletteListItem);
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Clear));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Distance));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnLeft));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnRight));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleUp));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleDown));
        menu.RefreshIndex();
        menu.Visible = true;
    }

    private static void OnListChange(UIMenu sender, UIMenuListItem list, int index)
    {
        if (DressingScript.Dressing == Main.Member.P2)
        {
            if (list.Text == sender.MenuItems[0].Text)
            {
                SkinHelper.SetPedComponentVariation(Main.P2.Character, cid, drawableListItem.Index, 0, 0);
                InitComponentSubMenu(cid);
            }
            SkinHelper.SetPedComponentVariation(Main.P2.Character, cid, drawableListItem.Index, textureListItem.Index, paletteListItem.Index);
            Main.P2.skin.Components[cid].action = true;
            return;
        }
        if (list.Text == sender.MenuItems[0].Text)
        {
            SkinHelper.SetPedComponentVariation(Main.P1.Character, cid, drawableListItem.Index, 0, 0);
            SkinHelper.SetPedComponentVariation(Main.Clone.Character, cid, drawableListItem.Index, 0, 0);
            InitComponentSubMenu(cid);
        }
        SkinHelper.SetPedComponentVariation(Main.P1.Character, cid, drawableListItem.Index, textureListItem.Index, paletteListItem.Index);
        SkinHelper.SetPedComponentVariation(Main.Clone.Character, cid, drawableListItem.Index, textureListItem.Index, paletteListItem.Index);
        Main.P1.skin.Components[cid].action = true;
        Main.Clone.skin.Components[cid].action = true;
    }

    public static void UpdateComponentsValue()
    {
        if (DressingScript.Dressing == Main.Member.P2)
        {
            Main.P2.skin.GetComponentsValue();
            Main.P2.skin.GetComponentsVariableNumber();
            return;
        }
        Main.P1.skin.GetComponentsValue();
        Main.P1.skin.GetComponentsVariableNumber();
        Main.Clone.skin.GetComponentsValue();
        Main.Clone.skin.GetComponentsVariableNumber();
    }

    private static void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
    {
        switch (index)
        {
            case 3:
            {
                drawableListItem.Index = 0;
                textureListItem.Index = 0;
                paletteListItem.Index = 0;
                if (DressingScript.Dressing == Main.Member.P2)
                {
                    SkinHelper.SetPedComponentVariation(Main.P2.Character, cid, drawableListItem.Index, textureListItem.Index, paletteListItem.Index);
                    Main.P2.skin.Components[cid].action = false;
                }
                else
                {
                    SkinHelper.SetPedComponentVariation(Main.P1.Character, cid, drawableListItem.Index, textureListItem.Index, paletteListItem.Index);
                    SkinHelper.SetPedComponentVariation(Main.Clone.Character, cid, drawableListItem.Index, textureListItem.Index, paletteListItem.Index);
                    Main.P1.skin.Components[cid].action = false;
                    Main.Clone.skin.Components[cid].action = false;
                }
                InitComponentSubMenu(cid);
                break;
            }
            case 4:
                DressingScript.Distance();
                break;
            case 5:
                DressingScript.TurnLeft();
                break;
            case 6:
                DressingScript.TurnRight();
                break;
            case 7:
                DressingScript.AngleUp();
                break;
            case 8:
                DressingScript.AngleDown();
                break;
        }
    }

    private static void OnMenuClose(UIMenu sender)
    {
        menu.Visible = false;
        menu.RefreshIndex();
        // 避免退格时执行更新导致的潜在阻塞
    }
}


